package com.zhhtech.zhh.ui.activity.commom

import android.content.Intent
import android.view.View
import com.zhhtech.zhh.BuildConfig
import com.zhhtech.zhh.R
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.common.UserCache
import com.zhhtech.zhh.databinding.ActivitySplashBinding
import com.zhhtech.zhh.ui.activity.main.MainActivity
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.dialog.SecretDialog
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.helper.KEY_IMEI
import com.zhhtech.zhh.ui.helper.getImeiSuspend
import com.zhhtech.zhh.ui.helper.uuid
import com.zhhtech.zhh.ui.utils.SPUtils
import kotlinx.coroutines.delay
import me.jessyan.autosize.internal.CancelAdapt

class SplashActivity : BaseActivity<ActivitySplashBinding>(), CancelAdapt {
    override fun initData() {
        if (!this.isTaskRoot) {
            val mainIntent = intent
            val action = mainIntent.action
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == action) {
                finish()
                return
            }
        }
        if (!SPUtils.getBoolean("sp_key_secret", false)) {
            val secretDialog = SecretDialog(this)
            secretDialog.mOnClickListener = ::onClick
            secretDialog.show()
        } else {
            initSdk()
        }
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.btn_cancel -> finish()
            R.id.btn_confirm -> {
                SPUtils.put("sp_key_secret", true)
                initSdk()
            }
        }
    }

    private fun initSdk() {
        com.zhhtech.zhh.App.init()
//        if (!SPUtils.getBoolean("key_guide")) {
//            launchStartActivityForResult(GuideActivity::class.java, false) {
//                SPUtils.put("key_guide", true)
        checkLogin()
//            }
//        } else {
//            checkLogin()
//        }
    }

    private fun checkLogin() {
        if (BuildConfig.VERSION_NAME.replace(".", "").toInt() <= 203) {
            val key = "IMEI_${BuildConfig.VERSION_NAME}_CLEAR"
            val flag = SPUtils.getBoolean(key, false)
            if (!flag) {
                SPUtils.remove(KEY_IMEI)
            }
            SPUtils.put(key, true)
        }
        val job = launchUI({
            getImeiSuspend(mThis)
//            runOnUiThread { (getImei()).toastMessageLong() }
            api.activate().toData()
            doCheckLogin()
        }, ApiObserver(error = {
            doCheckLogin()
        }))
        launchUI({
            delay(1000 * 9)
            if (!job.isCompleted) {
                job.cancel()
                SPUtils.put(KEY_IMEI, uuid())
                checkLogin()
            }
        })
    }

    private fun doCheckLogin() {
        if (UserCache.isLogin) {
            goMain()
        } else {
            launchStartActivityForResult(LoginActivity::class.java) {
                goMain()
            }
        }
    }

    private fun goMain() {
        startActivityExt<MainActivity>()
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        finish()
    }
}